<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/Backend for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Backend\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model\Sejour;
use Application\Model\SejourTable;

class SitemapController extends BackendHeadController
{
	protected $SejourTable;
	protected $SitemapService;
	protected $SitemapTable;

    public function indexAction()
    {
//     	if (!$this->getAuthService()->hasIdentity()){
//     		return $this->redirect()->toUrl("/backend/identification/index");
//     	}
    	
        return new ViewModel ();
    }

    public function viewAction() {
		$result = new ViewModel();
	    $result->setTerminal(true);

	    return $result;
    }

    public function sitemapgeneratorAction()
    {
    	$request = $this->getRequest();

    	if ( $request->isGet() ) {
			$reponse = $this->params()->fromQuery('lancer');

			if( $reponse == 'true') {
				$config = $this->getServiceLocator()->get('Config');
        		$root_url = $config['root_url'];
        		$urls_sitemap = $config['urls_sitemap'];

				$Sitemap = $this->getSitemapTable();
				$aSitemaps = $Sitemap->fetchAllArray();

				$obj = $this->getSitemapService();

        		
				// Génération des urls statics
				foreach ($urls_sitemap as $key => $value) {
					$obj->addItem($root_url . $value, 0.5, date('Y-m-d'), 'monthly');
				}
 
				// Génération des urls dynamiques
				foreach ($aSitemaps as $key => $value) {
					$obj->addItem($root_url . $value->urlSitemap, 0.5, date('Y-m-d'), 'monthly');
				}

				$xml = $obj->objToXML( $obj->getItems() );

				$obj->saveToFile($xml);
			}
    	} 

		return $this->response;
    }

    public function genererSitemapSejoursAction() {

		if ( $this->utilisateurConnecteEstAdmin() ) {

			$sejoursFiltres = $this->getSejourTable()->filtrerLesSejours();

			$this->getSitemapTable()->reinitialiserTable();

			foreach ($sejoursFiltres as $key => $sejour) {
				$this->getSitemapTable()->genererNouvelleEntreeSitemap( $sejour->id );
			}

			echo htmlentities("Combien de séjours analisés ") . count($sejoursFiltres) ;die;
		} else {
			return $this->notFoundAction();
		}
	}



    /**  **/

    public function getSitemapService()
	{
		if( !$this->SitemapService ){
			$sm = $this->getServiceLocator();
			$this->SitemapService = $sm->get("SitemapService");
		}

		return $this->SitemapService;
	}

	public function getSitemapTable()
	{
		if( !$this->SitemapTable ){
			$sm = $this->getServiceLocator();
			$this->SitemapTable = $sm->get("Application\Model\SitemapTable");
		}
		return $this->SitemapTable;
	}

	public function getSejourTable()
	{
		if( !$this->SejourTable ){
			$sm = $this->getServiceLocator();
			$this->SejourTable = $sm->get("Application\Model\SejourTable");
		}
		return $this->SejourTable;
	}
}
